﻿using System.Linq;
using System.Web.Routing;

namespace StampController
{
    public static class UrlRouting
    {
        public static void Initialize()
        {
            //RouteTable.Routes.Clear();
            RouteTable.Routes.RouteExistingFiles = true;
            RegisterRoutes(RouteTable.Routes);
        }

        private static void RegisterRoutes(RouteCollection routes)
        {
            // Do not use url routing for requests with .axd/.asmx/.ico extensions
            // This prevents ASP.NET from having to do File.Exists check
            routes.Add("ignoreAXD", new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreASMX", new Route("{resource}.asmx/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreICO", new Route("{resource}.ico/{*pathInfo}", new StopRoutingHandler()));
            
            // Added to solve a issue with http://(www.)domain.ext/ rendering 404
            routes.Add("Home", new Route("", new StopRoutingHandler()));

            //routes.Add("ImagesRoute",
            //    new Route("images/{filename}", new ImageRouteHandler()));
            
            //routes.Add("UploadRoute",
            //    new Route("upload/{filename}", new ImageRouteHandler()));

            //routes.Add("UploadRoute1",
            //    new Route("upload/{time}/{filename}", new ImageRouteHandler()));

            routes.Add("AuthenticateRoute",
                new Route("a/{id}", new AuthenticateRouteHandler()));
        }

        public static void AddRoute(Route route)
        {
            // Ensure route does not already exist
            if (RouteTable.Routes.Cast<Route>().Any(r => r.Url == route.Url))
            {
                return;
            }

            RouteTable.Routes.Add(route);
        }
    }
}